/*
 * @Description: 校验公共文件
 * @version:
 * @Author: HYH
 * @Date: 2022-08-23 11:33:19
 * @LastEditors: HYH
 * @LastEditTime: 2022-08-23 11:34:38
 */
let currentPassword = '' //定义一个空字符串接受密码
//#   手机号验证
export async function checkPhoneNum(rule: RegExp, value: string, callback: Function) {
  const regpCard = /^1(3|4|5|7|8)\d{9}$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
  // else if (!regpCard.test(value)) {
  //   callback(new Error(lang('mobileRegpWarn')))
  // }
}
//#    邮箱验证
export async function checkemail(rule: RegExp, value: string, callback: Function) {
  const regpEmail =
    /^[_a-z0-9\u4e00-\u9fa5]+(\.[_a-z0-9\u4e00-\u9fa5]+)*@[a-z0-9\u4e00-\u9fa5]+(\.[a-z0-9\u4e00-\u9fa5]+)*(\.[a-z]{2,})$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (!regpEmail.test(value)) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
}
//#    密码验证
export async function checkPassword(rule: RegExp, value: string, callback: Function) {
  currentPassword = value
  const regpPassword = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (!regpPassword.test(value)) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
}
//#    再次输入密码验证
export async function checkpasswordAgain(rule: RegExp, value: string, callback: Function) {
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (currentPassword !== value) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
}

/**输入的折扣限制 */
export async function checkDiscount(rule: RegExp, value: string, callback: Function) {
  const regp = /^[0-9]{1}(\.[0-9])?$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (!regp.test(value) || Number(value) === 0) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
}
/**输入的价格限制 */
export async function checkPrice(rule: RegExp, value: string, callback: Function) {
  const regpPrice = /^[\+\-]?(0|[1-9]\d{0,11}|0\.\d{1,2}|[1-9]\d{0,11}\.\d{1,2})$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (!regpPrice.test(value) || Number(value) === 0) {
    callback(new Error(lang('inputNotAllowed')))
  } else {
    callback()
  }
}
/**输入内容为数字和小数 */
export async function checkIfNumber(rule: RegExp, value: string, callback: Function) {
  const regp = /^(\-|\+)?\d+(\.\d+)?$/
  if (!value) {
    callback(new Error(lang('inputInfo')))
  } else if (!regp.test(value) || Number(value) === 0) {
    callback(new Error(lang('inputInfo')))
  } else {
    callback()
  }
}

// 给价格加入符号作为千分位， 默认使用逗号
export const formatPrice = (number: any, sign: any = ',') => {
  if (number) {
    var parts = number.toString().split('.')
    parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, sign)
    return parts.join('.')
  } else {
    return ''
  }
}
// 给价格(千分位)去除符号 (,)
export const delComma = (num: any) => {
  if (num === undefined || num === null || num === '') {
    return null
  }
  num = num.toString()
  num = num.replace(/[ ]/g, '') //去除空格
  num = num.replace(/,/gi, '')
  return Number(num)
}
